package cn.tedu.wallet.dao.repository.impl;

import cn.tedu.wallet.dao.mapper.RecordMapper;
import cn.tedu.wallet.dao.repository.RecordRepository;
import cn.tedu.wallet.pojo.po.UserTransactionRecordPO;
import cn.tedu.wallet.pojo.vo.UserTransactionRecordVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

@Repository
@Slf4j
public class RecordRepositoryImpl implements RecordRepository {

    @Autowired
    private RecordMapper recordMapper;

    @Override
    public void doAddRecord(UserTransactionRecordPO recordPO) {
        log.debug("开始执行添加交易记录操作");
        recordMapper.insert(recordPO);
        log.info("添加交易记录操作执行完成");
    }

    @Override
    public UserTransactionRecordPO getRecordDetail(Long recordId) {
        log.debug("开始执行查询交易记录细节操作{}", recordId);
        return recordMapper.selectById(recordId);
    }

    @Override
    public void updateRecord(UserTransactionRecordPO recordPO) {
        log.debug("开始执行更新交易记录操作{}", recordPO);
        recordMapper.updateById(recordPO);
        log.info("更新交易记录操作执行完成");
    }

    @Override
    public UserTransactionRecordPO doSelectArticlePayRecord(Long articleId, Long userId) {
        log.debug("开始执行查询文章支付记录操作{}", articleId);
        QueryWrapper<UserTransactionRecordPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("article_id", articleId)
                .eq("payer_user_id", userId);
        return recordMapper.selectOne(queryWrapper);
    }

    @Override
    public List<UserTransactionRecordPO> doSelectUserPayRecord(Long userId) {
        log.debug("开始执行查询用户交易记录操作{}", userId);
        QueryWrapper<UserTransactionRecordPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account_user_id", userId)
                .or()
                .eq("payer_user_id", userId)
                .orderByDesc("transaction_time");
//                .or(i -> i.eq("account_user_id", userId).ne("transaction_status", 3))
//                .or(i -> i.eq("payer_user_id", userId).ne("transaction_status", 3));
        List<UserTransactionRecordPO> list = recordMapper.selectList(queryWrapper);
//        List<UserTransactionRecordVO> recordVOList = new ArrayList<>();
//        for (UserTransactionRecordPO recordPO : list) {
//            UserTransactionRecordVO recordVO = new UserTransactionRecordVO();
//            BeanUtils.copyProperties(recordPO, recordVO);
//            recordVOList.add(recordVO);
//        }
        log.info("查询用户交易记录操作执行完成{}", list);
        return list;
    }
}
